<?php
// @formatter:off
/**
 * @file WCGroup.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-04-30 02:04:27 +0000 (Tue, 30 Apr 2013) $
 *
 * $Id: WCGroup.php 63 2013-04-30 02:04:27Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/representations/WCGroup.php $
 */
// @formatter:on

/**
 * @class WCGroup
 */
class WCGroup extends WCTableRepresentation {
	protected $_PropertiesReadOnly = array(
		"id",
		"root"
	);
	protected $_PropertiesBoolean = array("public");
	protected $_PropertiesPrefix = "grp_";
	protected $_PropertiesTable = "groups";

	public function __construct(&$groupsHolder, $id) {
		parent::__construct();

		$this->load($id);
	}
	//
	// Public methods
	public function add($directory) {
		$out = true;

		if(!$this->has($directory)) {
			$query = "insert\n";
			$query .= "        into {$this->_dbprefix}directories (dir_name, dir_group)\n";
			$query .= "        values (:directory, :group)";

			$stmt = $this->_db->prepare(__CLASS__."::".__FUNCTION__, $query);

			$params = array(
				":directory" => $directory,
				":group" => $this->id
			);

			if(!$stmt->execute($params)) {
				$out = false;
			}
		}

		return $out;
	}
	public function count() {
		$out = $this->count;
		if($this->ok) {
			if(!$out) {
				$stmt = null;
				$stmtId = __CLASS__."::".__FUNCTION__;
				if($this->_db->has($stmtId)) {
					$stmt = $this->_db->get($stmtId);
				} else {
					$query = "select  count(1) as dir_count\n";
					$query .= "from    {$this->_dbprefix}directories\n";
					$query .= "where   dir_group = :group\n";

					$stmt = $this->_db->prepare($stmtId, $query);
				}

				$params = array(":group" => $this->id);

				if($stmt->execute($params) && $stmt->rowCount() == 1) {
					$row = $stmt->fetch();
					$this->count = $row["dir_count"];
					$out = $this->count;
				} else {
					$out = false;
				}
			}
		} else {
			$out = false;
		}
		return $out;
	}
	/**
	 * @return multitype:int
	 */
	public function directories() {
		global $wcDirectoriesHolder;
		return $wcDirectoriesHolder->itemIds($this);
	}
	/**
	 * @return multitype:WCDirectory
	 */
	public function directoriesFull() {
		global $wcDirectoriesHolder;
		return $wcDirectoriesHolder->items($this);
	}
	public function has($dirId) {
		return in_array($dirId, $this->directories());
	}
	public function isPublic() {
		return $this->public;
	}
	public function remove() {
		die("disabled ".__FILE__.":".__LINE__);
	}
	public function removeDirectory(WCDirectory &$directory) {
		$out = false;

		if($this->has($directory->id) && $directory->name != "/") {
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "delete from  {$this->_dbprefix}directories\n";
				$query .= "where        dir_id    = :directory\n";
				$query .= " and         dir_group = :group";

				$stmt = $this->_db->prepare($stmtId, $query);
			}

			$params = array(
				":group" => $this->id,
				":directory" => $directory->id
			);

			$out = $stmt->execute($params) && $stmt->rowCount() > 0;
		}

		if($out) {
			$this->count--;
		}

		return $out;
	}
	public function root() {
		return $this->root;
	}
	public function setPublic($isPublic) {
		$this->public = $isPublic;
		$this->reload();
		return $this->isPublic();
	}
	public function trash() {
		return $this->trash;
	}
	//
	// Protected methods
}
?>